// const BASE_URL = 'https://www.xiteng.com/xitenggamejar/';
// const BASE_URL = 'http://47.94.209.108:7009/';
const BASE_URL = 'https://api.tuexing.com/';
// const loginUtil = require('./login.js')

var app = getApp();

function request(url, params, success, fail) {
  this.requestLoading(url, params, "", success, fail)
}
// 展示进度条的网络请求
// url:网络请求的url
// params:请求参数
// message:进度条的提示信息
// success:成功的回调函数
// fail：失败的回调
function requestLoading(url, params, message = '', success, fail) {

  //未登录accessInfo
  const TOKEN = app.globalData.accessToken;
  let accessToken = '' ;
  if (TOKEN!=null){
    accessToken = TOKEN ;
  }


  let finalParams = Object.assign(params);

  if (message != "") {
    wx.showLoading({
      title: message,
      mask: true
    })
  }
  wx.request({
      url: BASE_URL + url,
      data: params,
      header: {
        'Content-Type': 'application/json',
        'accessToken': accessToken
      },
      method: 'post',
      success: function(res) {
        if (message != "") {
          wx.hideLoading()
        }
        if (res.statusCode == 200 && res.data.status==='ok') {
          success(res.data)
        } else {
          wx.showToast({
            title: res.data.message,
          })
          fail && fail(res)
        }
    },
    fail: function(res) {
      if (message != "") {
        wx.hideLoading()
      }
      fail && fail(res);
    },
    complete: function(res) {}
  })
}

const getRequest = (url, params, success, fail)=>{


  //未登录accessInfo
  const TOKEN = app.globalData.accessToken;
  let accessToken = '';
  if (TOKEN != null) {
    accessToken = TOKEN;
  }
  wx.request({
    url: BASE_URL + url + '?' + params,
    header: {
      'Content-Type': 'application/json',
      'accessToken': accessToken
    },
    method: 'get',
    success: function (res) {
      if (res.statusCode == 200 && res.data.status === 'ok') {
        success(res.data)
      } else {
        fail && fail(res)
      }
    },
    fail: function (res) {
      fail && fail(res);
    },
    complete: function (res) { }
  })
}

module.exports = {
  request: request,
  requestLoading: requestLoading,
  getRequest: getRequest,
  BASE_URL: BASE_URL
}